package quq.missq.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.LinkedList;

import quq.missq.Constants;
import quq.missq.MLog;
import quq.missq.R;
import quq.missq.activity.BigShootDetailActivity;
import quq.missq.activity.BigShootDetailMediaActivity;
import quq.missq.activity.TaPageActivity;
import quq.missq.beans.BigShootBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 7/27/15.
 */
public class BigShootAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_SUBSCRIBLE = 1;

    private Context mContext;
    private LinkedList<BigShootBean.Result> results;
    private LayoutInflater mInflater;


    public BigShootAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<BigShootBean.Result> results) {
        this.results = results;
    }

    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
    }

    public LinkedList<BigShootBean.Result> getData() {
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View view = mInflater.inflate(R.layout.foot_layout, null);
            return new FootViewHolder(view);
        } else if (viewType == TYPE_SUBSCRIBLE) {
            View view = mInflater.inflate(R.layout.big_shoot_adapter, null);
            return new BigshootHolder(view);
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof BigshootHolder) {
            BigshootHolder holder = (BigshootHolder) viewHolder;
            final BigShootBean.Result result = results.get(position);
            int categoryId = result.getPost().getCategory().getId();
            int imgId = -1;
            if (categoryId == 2) {
                imgId = R.mipmap.icon_change;
            } else if (categoryId == 3) {
                imgId = R.mipmap.icon_plan;
            } else {
                imgId = R.mipmap.icon_night;
            }
            holder.typeIcon.setImageResource(imgId);
            holder.title.setText(result.getPost().getTitle());
            ImageLoadUtil.loadImage(holder.contentImage, Constants.IMAGE_HOST + result.getCover(), R.mipmap.default_empty2);
            holder.avtar.setImageUrl(Constants.IMAGE_HOST + result.getPost().getAuthor().getAvatar(), VolleyTool.getImageLoader());
            holder.avtar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("uid", result.getPost().getAuthor().getId());
                    intent.putExtra("username", result.getPost().getAuthor().getNkname());
                    intent.setClass(mContext, TaPageActivity.class);
                    mContext.startActivity(intent);
                }
            });
            final int mediaType = result.getPost().getMediaType();
            if (mediaType == 0) {
                holder.bofang.setVisibility(View.GONE);
            } else {
                holder.bofang.setVisibility(View.VISIBLE);
            }
            holder.name.setText(result.getPost().getAuthor().getNkname());
            holder.favored.setText(result.getPost().getFavors() + "");
            String genderS = "男";
            if (result.getPost().getAuthor().getGender() == 1) {
                genderS = "男";
            } else {
                genderS = "女";
            }
            holder.gender.setText(genderS);
            holder.level.setText("level " + result.getPost().getAuthor().getLevel());
//            holder.contentImage.setOnClickListener(new View.OnClickListener() {
//
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent();
//                    if (mediaType == 0) {
//                        intent.putExtra("headTitle", result.getPost().getTitle());
//                        intent.putExtra("path", result.getPost().getMediaPath());
//                        intent.setClass(mContext, BigShootDetailMediaActivity.class);
//                        intent.putExtra("id", result.getPost().getId());
//                        mContext.startActivity(intent);
//                    }
//                }
//            });
            holder.contentImage.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_UP:
                            Intent intent = new Intent();
                            if(mediaType == 0){
                                intent.setClass(mContext, BigShootDetailActivity.class);
                            }else{
                                intent.putExtra("headTitle", result.getPost().getTitle());
                                MLog.e(result.getPost().getMediaPath());
                                intent.putExtra("path",result.getPost().getMediaPath());
                                intent.setClass(mContext, BigShootDetailMediaActivity.class);
                            }
                            intent.putExtra("id", result.getPost().getId());
                            mContext.startActivity(intent);
                            break;
                    }

                    return true;
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    //if(mediaType == 0){
                    intent.setClass(mContext, BigShootDetailActivity.class);
//                    }else{
//                        intent.putExtra("headTitle",result.getPost().getTitle());
//                        intent.putExtra("path",result.getPost().getMediaPath());
//                        intent.setClass(mContext, BigShootDetailMediaActivity.class);
//                    }
                    intent.putExtra("id", result.getPost().getId());
                    mContext.startActivity(intent);
                }
            });
        } else if (viewHolder instanceof FootViewHolder)

        {
            FootViewHolder footHolder = (FootViewHolder) viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_SUBSCRIBLE;
        }
    }

    @Override
    public int getItemCount() {
        return results.size() + 1;
    }

    class BigshootHolder extends RecyclerView.ViewHolder {

        ImageView typeIcon;
        TextView title;
        ImageView contentImage;
        NetworkImageView avtar;
        TextView name;
        TextView gender;
        TextView level;
        TextView favored;
        ImageView bofang;

        public BigshootHolder(View itemView) {
            super(itemView);
            typeIcon = (ImageView) itemView.findViewById(R.id.typeIcon);
            bofang = (ImageView) itemView.findViewById(R.id.bofang);
            title = (TextView) itemView.findViewById(R.id.title);
            contentImage = (ImageView) itemView.findViewById(R.id.contentImage);
            avtar = (NetworkImageView) itemView.findViewById(R.id.avtar);
            name = (TextView) itemView.findViewById(R.id.name);
            gender = (TextView) itemView.findViewById(R.id.gender);
            level = (TextView) itemView.findViewById(R.id.level);
            favored = (TextView) itemView.findViewById(R.id.favored);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
